Ext.define('AmpleHarvest.objects.Statistics.view.pages.cStatus',
{
	extend			: 'AmpleHarvest.objects.Statistics.absPanel',
	uses			:
	[
		'AmpleHarvestPHP.DATA.Store.statistics.cStatus'
	],
	requires		:
	[
			'AmpleHarvest.objects.tips.graph.cLine',
			'Ext.container.AbstractContainer', 'Ext.form.field.Date'
	],
	title			: 'Status',
	iconCls			: 'window-statistics-line-chart',
	m_arr_Series	: new Array(),
	layout			:
	{
		type	: 'vbox',
		align	: 'stretch'
	},
	// frame : true,
	initComponent	: function()
	{
		var obj_CurDate = new Date();

		this
				.addSeries(AmpleHarvestPHP.DATA.Store.statistics.cStatus.C_STR_GROUP_INDEX_ERROR);
		this
				.addSeries(AmpleHarvestPHP.DATA.Store.statistics.cStatus.C_STR_GROUP_PROCESS_ERROR);
		this
				.addSeries(AmpleHarvestPHP.DATA.Store.statistics.cStatus.C_STR_GROUP_NEW);
		this
				.addSeries(AmpleHarvestPHP.DATA.Store.statistics.cStatus.C_STR_GROUP_PROCESSING);
		this
				.addSeries(AmpleHarvestPHP.DATA.Store.statistics.cStatus.C_STR_GROUP_PROCESSED);
		this
				.addSeries(AmpleHarvestPHP.DATA.Store.statistics.cStatus.C_STR_GROUP_INDEXING);
		this
				.addSeries(AmpleHarvestPHP.DATA.Store.statistics.cStatus.C_STR_GROUP_INDEXED);

		this.setStore(Ext.create('AmpleHarvestPHP.DATA.Store.statistics.cStatus'));

		this.m_obj_DateSelect = Ext.create('Ext.container.AbstractContainer',
				{
					layout			:
					{
						type			: 'hbox',
						defaultMargins	:
						{
							top		: 5,
							right	: 5,
							bottom	: 0,
							left	: 5
						}
					},

					m_obj_EndDate	: Ext.Date
							.add(obj_CurDate, Ext.Date.DAY, 1),
					m_obj_StartDate	: Ext.Date.add(obj_CurDate, Ext.Date.MONTH,
							-1
					),
					frame			: true,
					items			:
					[
							{
								xtype		: 'datefield',
								fieldLabel	: "Start Date",
								flex		: 1,
								labelWidth	: 75,
								value		: Ext.Date.add(obj_CurDate,
										Ext.Date.MONTH, -1
								),
								listeners	:
								{
									'change'	:
									{
										fn		: function(obj_This, obj_New,
												obj_Old, obj_Options)
										{
											this.m_obj_DateSelect
													.set_StartDate(obj_New);
										},
										scope	: this
									},
									'render'	: function(obj_This,
											obj_Options)
									{
										this.m_obj_DateSelect
												.set_StartDate(obj_This
														.getValue());
									},
									scope		: this
								}
							},
							{
								xtype		: 'datefield',
								fieldLabel	: "End Date",
								flex		: 1,
								labelWidth	: 75,
								value		: Ext.Date.add(obj_CurDate,
										Ext.Date.DAY, 1
								),
								listeners	:
								{
									'change'	:
									{
										fn		: function(obj_This, obj_New,
												obj_Old, obj_Options)
										{
											this.m_obj_DateSelect
													.set_EndDate(obj_New);
										},
										scope	: this
									},
									'render'	:
									{
										fn		: function(obj_This,
												obj_Options)
										{
											this.m_obj_DateSelect
													.set_EndDate(obj_This
															.getValue());
										},
										scope	: this
									}
								}
							},
							{
								xtype	: "button",
								text	: "update",
								handler	: this.updateData,
								scope	: this
							}
					],
					set_StartDate	: function(obj_Date)
					{
						this.m_obj_StartDate = obj_Date;
					},
					get_StartDate	: function()
					{
						return this.m_obj_StartDate;
					},
					set_EndDate		: function(obj_Date)
					{
						this.m_obj_EndDate = obj_Date;
					},
					get_EndDate		: function()
					{
						return this.m_obj_EndDate;
					}
				});

		this.updateData();

		this.m_obj_Tip = Ext.create('AmpleHarvest.objects.tips.graph.cLine', {});

		this.m_obj_Chart = Ext.create('Ext.chart.Chart',
		{
			flex		: 1,
			animate		: false,
			store		: this.getStore(),
			// mask : 'horizontal',
			listeners	:
			{
				select	:
				{
					fn	: function(obj_Chart, obj_Selection)
					{
						obj_Chart.setZoom(obj_Selection);
						obj_Chart.mask.hide();
					}
				}
			},

			legend		:
			{
				position	: 'top'
			},
			axes		:
			[
					{
						type			: 'Numeric',
						position		: 'left',
						label			:
						{
							renderer	: Ext.util.Format.numberRenderer('0,0')
						},
						fields			:
						[
								AmpleHarvestPHP.DATA.Store.statistics.cStatus.C_STR_GROUP_INDEX_ERROR,
								AmpleHarvestPHP.DATA.Store.statistics.cStatus.C_STR_GROUP_PROCESS_ERROR,
								AmpleHarvestPHP.DATA.Store.statistics.cStatus.C_STR_GROUP_NEW,
								AmpleHarvestPHP.DATA.Store.statistics.cStatus.C_STR_GROUP_PROCESSING,
								AmpleHarvestPHP.DATA.Store.statistics.cStatus.C_STR_GROUP_PROCESSED,
								AmpleHarvestPHP.DATA.Store.statistics.cStatus.C_STR_GROUP_INDEXING,
								AmpleHarvestPHP.DATA.Store.statistics.cStatus.C_STR_GROUP_INDEXED

						],
						title			: 'Document Count',
						grid			: true,
						majorTickSteps	: 10,
						minorTickSteps	: 5
						// ,
						// minimum : 0
					},
					// {
					// type : 'Time',
					// position : 'bottom',
					// fields : 'label',
					// title : 'Date',
					// dateFormat : 'Y-m-d H:i:s',
					// groupBy : 'year,month,day',
					// //aggregateOp : 'sum'//,
					//					
					// constrain : true,
					// fromDate : new Date('08/22/11 01:01:01'),
					// toDate : new Date('09/23/11 23:40:01')
					// }//,
					{
						type		: 'Category',
						position	: 'bottom',
						label		:
						{
							renderer	: Ext.util.Format.dateRenderer('d/M/Y H:i'),
							rotate		:
							{
								degrees	: 315
							}
						},
						fields		:
						[
							'label'
						],
						title		: 'Date'
					}
			],
			series		:
			[
					{
						gutter	: 100,
						type	: 'line',
						axis	: 'left',
						xField	: 'label',
						yField	: AmpleHarvestPHP.DATA.Store.statistics.cStatus.C_STR_GROUP_INDEX_ERROR,
						tips	: this.m_obj_Tip
					},
					{
						gutter	: 100,
						type	: 'line',
						axis	: 'left',
						xField	: 'label',
						yField	: AmpleHarvestPHP.DATA.Store.statistics.cStatus.C_STR_GROUP_PROCESS_ERROR,
						tips	: this.m_obj_Tip
					},
					{
						gutter	: 100,
						type	: 'line',
						axis	: 'left',
						xField	: 'label',
						yField	: AmpleHarvestPHP.DATA.Store.statistics.cStatus.C_STR_GROUP_NEW,
						tips	: this.m_obj_Tip
					},
					{
						gutter	: 100,
						type	: 'line',
						axis	: 'left',
						xField	: 'label',
						yField	: AmpleHarvestPHP.DATA.Store.statistics.cStatus.C_STR_GROUP_PROCESSING,
						tips	: this.m_obj_Tip
					},
					{
						gutter	: 100,
						type	: 'line',
						axis	: 'left',
						xField	: 'label',
						yField	: AmpleHarvestPHP.DATA.Store.statistics.cStatus.C_STR_GROUP_PROCESSED,
						tips	: this.m_obj_Tip
					},
					{
						gutter	: 100,
						type	: 'line',
						axis	: 'left',
						xField	: 'label',
						yField	: AmpleHarvestPHP.DATA.Store.statistics.cStatus.C_STR_GROUP_INDEXING,
						tips	: this.m_obj_Tip
					},
					{
						gutter	: 100,
						type	: 'line',
						axis	: 'left',
						xField	: 'label',
						yField	: AmpleHarvestPHP.DATA.Store.statistics.cStatus.C_STR_GROUP_INDEXED,
						tips	: this.m_obj_Tip
					}
			]
		}
		);
		this.callParent();
		this.addDocked(Ext.create("Ext.toolbar.Toolbar",
		{
			dock	: 'top',
			items	:
			[
					{
						text	: "View",
						iconCls	: 'toolbar-statistics-view',
						menu	:
						{
							margin	: '0 0 10 0',
							items	:
							[
									{
										text			: 'Index Error',
										checked			: true,
										checkHandler	: this.changeResults,
										scope			: this,
										m_str_Series	: AmpleHarvestPHP.DATA.Store.statistics.cStatus.C_STR_GROUP_INDEX_ERROR
									},
									{
										text			: 'Process Error',
										checked			: true,
										checkHandler	: this.changeResults,
										scope			: this,
										m_str_Series	: AmpleHarvestPHP.DATA.Store.statistics.cStatus.C_STR_GROUP_PROCESS_ERROR
									},
									{
										text			: 'New',
										checked			: true,
										checkHandler	: this.changeResults,
										scope			: this,
										m_str_Series	: AmpleHarvestPHP.DATA.Store.statistics.cStatus.C_STR_GROUP_NEW
									},

									{
										text			: 'Processing',
										checked			: true,
										checkHandler	: this.changeResults,
										scope			: this,
										m_str_Series	: AmpleHarvestPHP.DATA.Store.statistics.cStatus.C_STR_GROUP_PROCESSING
									},

									{
										text			: 'Processed',
										checked			: true,
										checkHandler	: this.changeResults,
										scope			: this,
										m_str_Series	: AmpleHarvestPHP.DATA.Store.statistics.cStatus.C_STR_GROUP_PROCESSED
									},

									{
										text			: 'Indexing',
										checked			: true,
										checkHandler	: this.changeResults,
										scope			: this,
										m_str_Series	: AmpleHarvestPHP.DATA.Store.statistics.cStatus.C_STR_GROUP_INDEXING
									},

									{
										text			: 'Indexed',
										checked			: true,
										checkHandler	: this.changeResults,
										scope			: this,
										m_str_Series	: AmpleHarvestPHP.DATA.Store.statistics.cStatus.C_STR_GROUP_INDEXED
									}
							]
						}
					}
			]
		}
		));
		this.getStore().on("beforeload", this.showMask, this);
		this.getStore().on("load", this.hideMask, this);
		this.on("render", this.updateData, this);

		this.add(this.m_obj_DateSelect);
		this.add(this.m_obj_Chart);
	},
	changeResults	: function(obj_Item, bool_Checked)
	{
		if (bool_Checked)
		{
			this.addSeries(obj_Item.m_str_Series);
		}
		else
		{
			this.removeSeries(obj_Item.m_str_Series);
		}

		this.updateData();
	},
	addSeries		: function(str_Name)
	{
		if (!this.m_arr_Series)
		{
			this.m_arr_Series = new Array();
		}
		for (int_Key in this.m_arr_Series)
		{
			if (this.m_arr_Series[int_Key] == str_Name)
			{
				return;
			}
		}
		this.m_arr_Series.push(str_Name);
	},
	removeSeries	: function(str_Name)
	{
		for (int_Key in this.m_arr_Series)
		{
			if (this.m_arr_Series[int_Key] == str_Name)
			{
				this.m_arr_Series.splice(int_Key, 1);
				return;
			}
		}
	},
	updateData		: function()
	{
		var obj_StartDate = this.m_obj_DateSelect.get_StartDate();
		var obj_EndDate = this.m_obj_DateSelect.get_EndDate();
		var str_SubType = "";
		var int_DateDelta = 0;

		int_DateDelta = Ext.Date.getElapsed(obj_StartDate, obj_EndDate);

		if (int_DateDelta < (4 * 24 * 60 * 60 * 1000))
		{
			str_SubType = AmpleHarvestPHP.DATA.Store.statistics.cMetrics.C_STR_SUBTYPE_HOURLY;
		}
		else if ((int_DateDelta < (35 * 24 * 60 * 60 * 1000)))
		{
			str_SubType = AmpleHarvestPHP.DATA.Store.statistics.cMetrics.C_STR_SUBTYPE_DAILY;
		}
		else if ((int_DateDelta < (400 * 24 * 60 * 60 * 1000)))
		{
			str_SubType = AmpleHarvestPHP.DATA.Store.statistics.cMetrics.C_STR_SUBTYPE_MONTHLY;
		}
		else
		{
			str_SubType = AmpleHarvestPHP.DATA.Store.statistics.cMetrics.C_STR_SUBTYPE_YEARLY;
		}

		this.getStore().setSeries(this.m_arr_Series);
		this.getStore().setStartDate(this.m_obj_DateSelect.m_obj_StartDate);
		this.getStore().setEndDate(this.m_obj_DateSelect.m_obj_EndDate);
		this.getStore().setSubType(str_SubType);

		this.getStore().load();
	}
}
);
